﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace VSM.Business.DataCenter
{
    public partial class ItemMasterData : System.Web.UI.Page
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {

                }
            }
            catch (Exception ex)
            {
                this.lblError.Text = ex.Message;
                this.lblError.Visible = true;
            }
        }
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Timer1_Tick(object sender, EventArgs e)
        {
            string dbType = (string)Session[Utils.AppConstants.ID_Dealer] == "VSM" ? Utils.AppConstants.VSM : Utils.AppConstants.DEALER;
            GeneralFunctions GF = new GeneralFunctions(dbType, Session[Utils.AppConstants.ID_Dealer].ToString());
            DataTable dtWhs = GF.GetWarehouseByDealerDepartment(dbType, Session[Utils.AppConstants.ID_Dealer].ToString(), Utils.AppConstants.PARTS);
            if (dtWhs != null)
            {
                this.dllWareHouse.DataSource = dtWhs;
                this.dllWareHouse.DataValueField = "WhsCode";
                this.dllWareHouse.DataTextField = "WhsName";
                this.dllWareHouse.DataBind();
            }
            LoadItemData();
            Timer1.Enabled = false;
        }
        /// <summary>
        /// 
        /// </summary>
        private void LoadItemData()
        {
            try
            {
                string dbType = (string)Session[Utils.AppConstants.ID_Dealer] == "VSM" ? Utils.AppConstants.VSM : Utils.AppConstants.DEALER;
                MasterData obj = new MasterData();
                DataSet ds = obj.LoadCustomItem(Session[Utils.AppConstants.ID_Dealer].ToString(), dbType, Utils.AppConstants.PARTS);
                if (ds != null)
                {
                    Session["ItemMasterData"] = ds;
                    this.grdItem.DataSource = ds;
                    this.grdItem.DataBind();
                }
            }
            catch (Exception ex)
            {
                this.lblError.Text = ex.Message;
                this.lblError.Visible = true;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdItem_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "MouseEvents(this, event)");
                e.Row.Attributes.Add("onmouseout", "MouseEvents(this, event)");
                e.Row.Attributes["style"] = "cursor:pointer";
                e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.grdItem, "Select$" + e.Row.RowIndex);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdItem_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                this.hdnIsUpdate.Value = "";
                GridViewRow row = this.grdItem.SelectedRow;
                DataTable dt = ((DataSet)Session["ItemMasterData"]).Tables[0];
                if (dt != null)
                {
                    int index = row.DataItemIndex;
                    var data = dt.Rows[index];
                    if (data != null)
                    {
                        this.txtItemCode.Text = data["ItemCode"].ToString();
                        this.txtItemName.Text = data["ItemName"].ToString();
                        this.txtForeignName.Text = data["FrgnName"].ToString();
                        this.txtOnHand.Text = data["OnHand"].ToString();
                        this.txtUnitPrice.Text = data["Price"].ToString();
                        this.dllWareHouse.SelectedValue = data["WhsCode"].ToString();
                        this.dllPriceList.SelectedValue = data["PriceList"].ToString();
                        this.hdnDocEntry.Value = data["DocEntry"].ToString();
                        this.hdnIsUpdate.Value = "1";
                        this.txtItemCode.Enabled = false;
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblError.Text = ex.Message;
                this.lblError.Visible = true;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.txtItemCode.Text == "" ||
                      this.txtItemName.Text == "")
                {
                    this.lblError.Text = "Missing Data.";
                    this.lblError.Visible = true;
                    return;
                }
                DataSet dsUpdate = new DataSet();
                DataTable oitm = new DataTable("OITM");
                oitm.Columns.Add("ItemCode");
                oitm.Columns.Add("ItemName");
                oitm.Columns.Add("FrgnName");
                oitm.Columns.Add("ItmsGrpCod");
                oitm.Columns.Add("U_DealerID");

                DataRow r = oitm.NewRow();
                r["ItemCode"] = this.txtItemCode.Text;
                r["ItemName"] = this.txtItemName.Text;
                r["FrgnName"] = this.txtForeignName.Text;
                r["ItmsGrpCod"] = "102";
                r["U_DealerID"] = Session[Utils.AppConstants.ID_Dealer].ToString();
                oitm.Rows.Add(r);


                DataTable oitw = new DataTable("OITW");
                oitw.Columns.Add("WhsCode");
                oitw.Columns.Add("ItemCode");
                oitw.Columns.Add("OnHand");

                r = oitw.NewRow();
                r["WhsCode"] = this.dllWareHouse.SelectedValue;
                r["OnHand"] = this.txtOnHand.Text;
                r["ItemCode"] = this.txtItemCode.Text;
                oitw.Rows.Add(r);


                DataTable itm1 = new DataTable("ITM1");
                itm1.Columns.Add("ItemCode");
                itm1.Columns.Add("PriceList");
                itm1.Columns.Add("Price");
                itm1.Columns.Add("Currency");

                r = itm1.NewRow();
                r["PriceList"] = this.dllPriceList.SelectedValue;
                r["Price"] = this.txtUnitPrice.Text;
                r["Currency"] = "VND";
                r["ItemCode"] = this.txtItemCode.Text;
                itm1.Rows.Add(r);

                DataTable itm12 = new DataTable("ITM12");
                itm12.Columns.Add("UomEntry");

                r = itm12.NewRow();
                r["UomEntry"] = "-1";
                itm12.Rows.Add(r);


                DataSet ds = new DataSet();
                ds.Tables.Add(oitm);
                if (this.hdnIsUpdate.Value.Length == 0)
                {
                    ds.Tables.Add(oitw);
                    ds.Tables.Add(itm1);
                    ds.Tables.Add(itm12);
                }

                string dbType = (string)Session[Utils.AppConstants.ID_Dealer] == "VSM" ? Utils.AppConstants.VSM : Utils.AppConstants.DEALER;
                DocumentXML docXml = new DocumentXML();
                string xml = docXml.ToXMLStringFromDS("4", ds);

                Transaction ts = new Transaction();
                bool isUpdate = false;
                if (this.hdnIsUpdate.Value == "1")
                {
                    isUpdate = true;
                }
                DataSet dsRet = ts.CreateMarketingDocument(xml, "4", this.txtItemCode.Text, isUpdate, dbType);
                if ((int)dsRet.Tables[0].Rows[0]["ErrCode"] != 0)
                {
                    this.lblError.Text = GeneralFunctions.UrlFullEncode(dsRet.Tables[0].Rows[0]["ErrMsg"].ToString());
                }
                else
                {
                    this.lblError.Text = "Operation complete sucessful!";
                    ResetForm();
                    LoadItemData();
                }
            }
            catch (Exception ex)
            {
                this.lblError.Text = ex.Message;
            }
            this.lblError.Visible = true;
        }
        /// <summary>
        /// 
        /// </summary>
        private void ResetForm()
        {
            this.txtItemCode.Text = "";
            this.txtItemName.Text = "";
            this.txtForeignName.Text = "";
            this.txtOnHand.Text = "0";
            this.txtUnitPrice.Text = "0";
            this.hdnIsUpdate.Value = string.Empty;
            this.grdItem.SelectedIndex = -1;
            this.txtItemCode.Enabled = true;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            ResetForm();
        }
    }
}